package com.changgou.order.controller;

import com.changgou.order.feign.CartFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * 老大哥在看着你
 *
 * @author ANG.YU
 * @date 2019-10-22 15:42
 * 处理购物车请求
 */
@Controller
@RequestMapping("/wcart")
public class CartController {
    @Autowired
    private CartFeign cartFeign;
    @Autowired
    public static final String WEB_URL = "http://web.changgou.com:9102/wo/wcart/tocart";

    /**
     * 将商品添加到购物车，并跳转到购物车页面
     */
    @GetMapping("/addGoodsToCart")
    public String addGoodsToCart(@RequestParam(name = "skuId") String skuId, @RequestParam(name = "num") Integer num ) {

        //1 将所选商品添加到购物车
        cartFeign.addGoodsToCart(skuId, num);
        return "redirect:" + WEB_URL;
    }

    @GetMapping("/tocart")
    public String tocart(Model model) {
        //2 将购物车内的商品查询出，来进行回显
        Map map = cartFeign.findCartList();
        model.addAttribute("result", map);
        return "cart";
    }
    /**更新复选框选中状态*/
    @GetMapping("/updateChecked")
    public String updateChecked(@RequestParam(name = "skuId") String skuId, @RequestParam(name = "checked") boolean checked) {
        cartFeign.updateChecked(skuId, checked);
        return "redirect:" + WEB_URL;
    }

    public static String getUsername() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return name;
    }
}
